package com.wmlive.hhvideo.heihei.discovery.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.wmlive.hhvideo.common.base.DcBaseActivity;
import com.wmlive.hhvideo.common.navigator.Navigator;
import com.wmlive.hhvideo.common.network.HttpConstant;
import com.wmlive.hhvideo.heihei.beans.discovery.BannerListBean;
import com.wmlive.hhvideo.heihei.beans.discovery.TopicTypeListBean;
import com.wmlive.hhvideo.heihei.beans.main.MultiTypeVideoBean;
import com.wmlive.hhvideo.heihei.beans.main.ShortVideoItem;
import com.wmlive.hhvideo.heihei.discovery.adapter.DiscoveryAdapter;
import com.wmlive.hhvideo.heihei.discovery.presenter.DiscoveryPresenter;
import com.wmlive.hhvideo.heihei.mainhome.fragment.RecommendFragmentNew;
import com.wmlive.hhvideo.heihei.mainhome.activity.VideoListActivity;
import com.wmlive.hhvideo.utils.CollectionUtil;
import com.wmlive.hhvideo.utils.DeviceUtils;
import com.wmlive.hhvideo.utils.imageloader.GlideLoader;
import com.wmlive.hhvideo.widget.SearchView;
import com.wmlive.hhvideo.widget.banner.BannerView;
import com.wmlive.hhvideo.widget.refreshrecycler.OnFooterClickListener;
import com.wmlive.hhvideo.widget.refreshrecycler.RefreshRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.wmlive.hhvideo.R;

/**
 * Created by lsq on 6/1/2017.
 * 发现页面
 */
public class DiscoveryActivity extends DcBaseActivity<DiscoveryPresenter> implements
        SwipeRefreshLayout.OnRefreshListener,
        RefreshRecyclerView.OnLoadMoreListener,
        DiscoveryAdapter.OnPictureClickListener,
        DiscoveryPresenter.IDiscoveryView {

    @BindView(R.id.recyclerView)
    RefreshRecyclerView recyclerView;
    private DiscoveryAdapter discoveryAdapter;
    private BannerView<BannerListBean.Banner> bannerView;

    @Override
    protected DiscoveryPresenter getPresenter() {
        return new DiscoveryPresenter(this);
    }

    @Override
    protected void onSingleClick(View v) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_discovery;
    }

    @Override
    protected void initData() {
        super.initData();
        setTitle("", true);
        SearchView searchView = (SearchView) toolbar.addCenterView(R.layout.view_discovery_search,
                null, Toolbar.LayoutParams.MATCH_PARENT).findViewById(R.id.searchView);
        searchView.setSearchMode(false);
        searchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(DiscoveryActivity.this, SearchActivity.class));
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        discoveryAdapter = new DiscoveryAdapter(new ArrayList<TopicTypeListBean.TopicListBean>(), recyclerView);
        discoveryAdapter.setOnPictureClickListener(this);
        bannerView = (BannerView) LayoutInflater.from(this).inflate(R.layout.include_discovery_banner, null);
        recyclerView.setAdapter(discoveryAdapter);
        recyclerView.setOnRefreshListener(this);
        recyclerView.setOnLoadMoreListener(this);
        recyclerView.setOnFooterClickListener(new OnFooterClickListener() {
            @Override
            public void onPageErrorClick() {
                super.onPageErrorClick();
                presenter.getTopicList(true);
            }

            @Override
            public void onFootErrorClick() {
                super.onFootErrorClick();
                presenter.getTopicList(false);
            }
        });
        recyclerView.setHeader(bannerView);
        recyclerView.autoRefresh(500);
    }

    //设置轮播图
    private void initBannerView() {
        bannerView.setViewFactory(new BannerViewFactory());
        bannerView.setOnItemClickListener(new BannerView.OnItemClickListener<BannerListBean.Banner>() {
            @Override
            public void onItemClick(int position, BannerListBean.Banner o) {
                if (null != o) {
                    Navigator.getInstance().bannerLink(DiscoveryActivity.this, o.getLink());
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != bannerView) {
            bannerView.resume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (null != bannerView) {
            bannerView.stop();
        }
    }

    @Override
    public void onPictureClick(long topicId, List<ShortVideoItem> listBean, long videoId, int position) {
        VideoListActivity.startActivity(this, RecommendFragmentNew.TYPE_TOPIC, MultiTypeVideoBean.createTopicParma(topicId, position, listBean));
    }

    @Override
    public void onTopicClick(int position, boolean isTopic, long id) {
        if (id > 0) {
            if (isTopic) {
                TopicActivity.startTopicActivity(this, id);
            } else {
                TopicActivity.startMusicActivity(this, id);
            }
        } else {
            showToast(R.string.hintErrorDataDelayTry);
        }
    }

    @Override
    public void onRequestDataError(int requestCode, String message) {
        if (requestCode == HttpConstant.TYPE_DISCOVERY_BANNER) {
            if (null != bannerView) {
                bannerView.setVisibility(View.GONE);
            }
        } else if (requestCode == HttpConstant.TYPE_DISCOVERY_TOPIC_HOME
                || requestCode == (HttpConstant.TYPE_DISCOVERY_TOPIC_HOME + 1)) {
            discoveryAdapter.showError(requestCode == HttpConstant.TYPE_DISCOVERY_TOPIC_HOME);
        }
    }

    @Override
    public void onBannerOk(final List<BannerListBean.Banner> bannerList) {
        // TODO: 6/2/2017 这个是测试数据
//        List<BannerListBean.Banner> list = new ArrayList<>();
//        for (int i = 0; i < urls.length; i++) {
//            BannerListBean.Banner item = new BannerListBean.Banner();
//            item.setCover(urls[i]);
//            list.add(item);
//        }
//        initBannerView();
//        bannerView.setDataList(list);
//        bannerView.start();

        if (!CollectionUtil.isEmpty(bannerList)) {
            bannerView.setVisibility(View.VISIBLE);
            initBannerView();
            bannerView.setDataList(bannerList);
            bannerView.start();
        } else {
            bannerView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onGetTopicListOk(boolean isRefresh, List<TopicTypeListBean.TopicListBean> list, boolean hasMore) {
        discoveryAdapter.addData(isRefresh, list, hasMore);
    }

    private class BannerViewFactory implements BannerView.ViewFactory<BannerListBean.Banner> {
        @Override
        public View create(BannerListBean.Banner item, int position, ViewGroup container) {
            ImageView iv = new ImageView(container.getContext());
//            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            GlideLoader.loadCornerImage(item.getCover(), iv, DeviceUtils.dip2px(DiscoveryActivity.this, 5));
            return iv;
        }
    }


    @Override
    public void onRefresh() {
        presenter.getBanner();
        presenter.getTopicList(true);
    }

    @Override
    public void onLoadMore() {
        presenter.getTopicList(false);
    }
}
